#include "client.h"


int send_message(int socket, Message *msg) {
    // 将消息类型和长度转换为网络字节序
    int type = htonl(msg->type);
    int length = htonl(msg->length);

    // 发送消息类型
    if (write(socket, &type, sizeof(int)) != sizeof(int)) {
        return -1;
    }

    // 发送消息长度
    if (write(socket, &length, sizeof(int)) != sizeof(int)) {
        return -1;
    }

    // 发送消息数据
    if (write(socket, msg->data, msg->length) != msg->length) {
        return -1;
    }

    return 0;
}